package com.qf.fengmimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fengmimall.product.entity.ProductComments;
import com.qf.fengmimall.product.mapper.ProductCommentsMapper;
import com.qf.fengmimall.product.service.IProductCommentsService;
import com.qf.fengmimall.user.entity.Users;
import com.qf.fengmimall.user.mapper.UsersMapper;
import com.qf.fengmimall.vo.ProductDetailCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author lx
 * @since 2023-02-15
 */
@Service
@Slf4j
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {

    @Autowired
    private ProductCommentsMapper productCommentsMapper;

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 获取商品评论信息
     *
     * @param id
     * @param pageNum
     * @param limit
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Map<String, Object> pageProductDetailCommonts(String id, Integer pageNum, Integer limit) {
        //创建分页对象
        Page<ProductComments> page = new Page<>(pageNum, limit);
        Page<ProductComments> productCommentsPages = productCommentsMapper.selectPage(page, new LambdaQueryWrapper<ProductComments>().eq(ProductComments::getProductId, id));
        List<ProductComments> productComments = productCommentsPages.getRecords();
        //组装用户信息
        assemble(productComments);

        Map<String, Object> map = new HashMap<>();
        map.put("list", productComments);
        map.put("count", productCommentsPages.getTotal());
        map.put("pageCount", productCommentsPages.getPages());
        return map;
    }

    /**
     * 商品评论
     *
     * @param id
     * @return
     */
    @Override
    public ProductDetailCountVo getProductDetailCount(String id) {
        //根据商品id查询用户评论
        List<Map<String, Object>> mapList = listMaps(new QueryWrapper<ProductComments>().select("comm_type,COUNT(1) as count").eq("product_id", id).groupBy("comm_type"));

        Long goodTotal = 0l;
        Long badTotal = 0l;
        Long midTotal = 0l;

        for (Map<String, Object> map : mapList) {
            Integer type = (Integer) map.get("comm_type");
            if (type.equals(1)) {
                goodTotal = (Long) map.get("count");
            } else if (type.equals(0)) {
                midTotal = (Long) map.get("count");
            } else {
                badTotal = (Long) map.get("count");
            }
        }
        Long total = goodTotal + badTotal + midTotal;

        String percent = BigDecimal.valueOf(goodTotal).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(total), 2, RoundingMode.HALF_UP).toString();


        return new ProductDetailCountVo(goodTotal, total, badTotal, midTotal, percent);
    }

    /**
     * 组装用户用户信息
     *
     * @param productComments
     */
    private void assemble(List<ProductComments> productComments) {
        //获取评论中用户id
        List<String> userIds = productComments.stream().map(ProductComments::getUserId).collect(Collectors.toList());
        //根据用户id查询用户
        List<Users> users = usersMapper.selectList(new LambdaQueryWrapper<Users>().in(Users::getUserId, userIds));
        //转换成map   id为key user为value
        Map<Integer, Users> userMap = users.stream().collect(Collectors.toMap(Users::getUserId, user -> user));

        productComments.forEach(o -> {
            String userId = o.getUserId();
            Users user = userMap.get(Integer.valueOf(userId));
            o.setUsername(user.getUsername());
            o.setNickname(user.getNickname());
            o.setUserImg(user.getUserImg());
        });
    }
}
